<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Models\Mainborad;
use App\Models\MainboradSetting;

class MainboradController extends Controller
{
        /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, Mainborad $mainborad)
    {
        $mainborads = $mainborad->filter($request->all())->paginate(config('api.page.limit'));
        $name = $request->name;
        return view('admins.mainborad.index',compact('mainborads','name'));
    }

    public function detail(Request $request, Mainborad $mainborad)
    {
        return response()->json(['code' => 200, 'msg' => 'success', 'data' => $mainborad->find($request->id)]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admins.mainborad.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, Mainborad $mainborad)
    {
        $result = $mainborad->create($request->all());
        if(!$result){
            return back()->with('warning','添加失败');
        }
        return redirect()->route('admin.mainborads.index')->with('success', '添加成功');
    }
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Mainborad $mainborad)
    {
        dd($mainborad);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Mainborad $mainborad)
    {
        return view('admins.mainborad.edit', compact('mainborad'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Mainborad $mainborad)
    {
        $result = $mainborad->update($request->all());
        if(!$result){
            return back()->with('warning','编辑失败');
        }
        return redirect()->route('admin.mainborads.index')->with('success', '编辑成功');
    }

    public function lists(Request $request, Mainborad $mainborad)
    {
        return json_encode(['code' => 200, 'msg' => 'success', 'data' => $mainborad->orderBy('id', 'desc')->get()]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete(Mainborad $mainborad)
    {
        $mainborad->delete();
        return json_encode(['code' => 200, 'message' => '删除成功']);
    }

    /**
     * [主板配置信息]
     * @param  Mainborad $mainborad [description]
     * @return [type]               [description]
     */
    public function config(Mainborad $mainborad)
    {
        $mainborad = $mainborad->load('config');
        return view('admins.mainborad.config', compact('mainborad'));
    }
    /**
     * [添加配置项]
     * @param  Request   $request   [description]
     * @param  Mainborad $mainborad [description]
     * @return [type]               [description]
     */
    public function configStore(Request $request, Mainborad $mainborad)
    {
        $config = $request->config;
        foreach ($config as $key => $value) {
            $result = $mainborad->config()->updateOrCreate($value);
        }
        //$result = $mainborad->config()->saveMany($data);
        if(!$result){
            return back()->with('warning','编辑失败');            
        }
        return redirect()->route('admin.mainborads.index')->with('success', '添加成功');
    }


    public function configDestroy(Request $request)
    {
        dd($request->all());
    }


    public function configLists(Request $request, Mainborad $mainborad)
    {
        $mainborad = $mainborad->with('config')->find($request->id);
        return $this->respondWithData(optional($mainborad)->config, 200, 'success');
    }

    public function list(Request $request, Mainborad $mainborad){
        $mainborads = $mainborad->get();
        return json_encode(['code' => 200, 'msg' => 'success', 'data' => $mainborads]);
    }

}
